﻿@page "/autocomplete"
<h3>AutoCompleteUsing</h3>

<div class="row">
    <div class="col-md-5 col-sm-12">
        <AutoComplete @bind-Value="customerName"
                      TItem="Customer"
                      DataProvider="CustomersDataProvider"
                      PropertyName="CustomerName"
                      Placeholder="Search a customer..."
                      StringComparison="StringComparison.Ordinal"
                      OnChanged="(Customer customer) => OnAutoCompleteChanged(customer)" />
    </div>
    <div class="col-md-5 col-sm-12">
        <InputText @bind-Value="customerName" />
    </div>
</div>
<div class="row">
    <div class="col-md-5 col-sm-12">
        <EditForm EditContext="@_editContext" OnValidSubmit="HandleOnValidSubmit">
            <DataAnnotationsValidator></DataAnnotationsValidator>

            <div class="form-group row mb-2">
                <label class="col-md-2 col-form-label">Customer:</label>
                <div class="col-md-10">
                    <AutoComplete @bind-Value="customerAddress.CustomerName"
                                  TItem="Customer2"
                                  DataProvider="CustomersDataProvider"
                                  PropertyName="CustomerName"
                                  Placeholder="Search a customer..."
                                  OnChanged="(Customer2 customer) => OnAutoCompleteChanged(customer)" />
                    <ValidationMessage For="@(() => customerAddress.CustomerName)" />
                </div>
            </div>

            <div class="form-group row mb-3">
                <label class="col-md-2 col-form-label">Address:</label>
                <div class="col-md-10">
                    <InputText class="form-control" @bind-Value="customerAddress.Address" />
                    <ValidationMessage For="@(() => customerAddress.Address)" />
                </div>
            </div>

            <div class="row">
                <div class="col-md-12 text-right">
                    <button type="submit" class="btn btn-success float-right">Submit</button>
                </div>
            </div>
        </EditForm>
    </div>
</div>
@code {

    private string? customerName;
    public IEnumerable<Customer>? customers;
    private EditContext? _editContext;

    private CustomerAddress customerAddress = new();

    protected override void OnInitialized()
    {
        _editContext = new EditContext(customerAddress);
        base.OnInitialized();
    }

    private async Task<AutoCompleteDataProviderResult<Customer>> CustomersDataProvider(AutoCompleteDataProviderRequest<Customer> request)
    {
        if (customers is null)
        {
            customers = GetCustomers();
        }
        return await Task.FromResult(request.ApplyTo(customers.OrderBy(c => c.CustomerName)));
    }

    private void OnAutoCompleteChanged(Customer customer)
    {
        // TODO: handle your own logic

        // NOTE: do null check
        Console.WriteLine($"'{customer?.CustomerName}' selected.");

        Console.WriteLine(customerName);
    }

    private IEnumerable<Customer> GetCustomers()
    {
        return new List<Customer> {
            new(1, "Pich S"),
            new(2, "sfh Sobi"),
            new(3, "Jojo chan"),
            new(4, "Jee ja"),
            new(5, "Rose Canon"),
            new(6, "Manju A"),
            new(7, "Bandita PA"),
            new(8, "Sagar Adil"),
            new(9, "Isha Wang"),
            new(10, "Daina JJ"),
            new(11, "Komala Mug"),
            new(12, "Dikshita BD"),
            new(13, "Neha Gosar"),
            new(14, "Preeti S"),
            new(15, "Sagar Seth"),
            new(16, "Vinayak MM"),
            new(17, "Vijaya Lakhsmi"),
            new(18, "Jahan K"),
            new(19, "Joy B"),
            new(20, "Zaraiah C"),
            new(21, "Laura L"),
            new(22, "Punith ES")
        };
    }

    public void OnValidSubmit()
    {
        Console.WriteLine($"Customer name is {customerAddress.CustomerName} and address is {customerAddress.Address}");
    }

  
}
